/*
 * Vector3f.cpp
 *
 *  Created on: Apr 29, 2009
 *      Author: wrice
 */
#include "MyVideoTrace.h"
#include <math.h>
namespace MyVideoTrace {

Vector3f::Vector3f( float x, float y, float z )
: x_(x), y_(y), z_(z)
{
}

float Vector3f::getX() const {
	return x_;
}

float Vector3f::getY() const {
	return y_;
}

float Vector3f::getZ() const {
	return z_;
}

Vector3f Vector3f::normalized() const {
	return Vector3f( *this ) / getLength();
}

float Vector3f::getLength() const {
	return sqrt( getX() * getX() + getY() * getY() + getZ() * getZ() );
}

Vector3f Vector3f::operator+ ( Vector3f rhs ) const {
	return Vector3f( getX() + rhs.getX(), getY() + rhs.getY(), getZ() + rhs.getZ() );
}

Vector3f Vector3f::operator/ (float scalar) const {
	return Vector3f( getX() / scalar, getY() /scalar, getZ() / scalar );
}

Vector3f Vector3f::operator- ( Vector3f rhs ) const {
	return Vector3f( getX() - rhs.getX(), getY() - rhs.getY(), getZ() - rhs.getZ() );
}

Vector3f Vector3f::operator* (float scalar) const {
	return Vector3f( getX() * scalar, getY() * scalar, getZ() * scalar );
}


float Vector3f::dotProduct( Vector3f rhs ) const {
	return getX() * rhs.getX() + getY() * rhs.getY() + getZ() * rhs.getZ();
}

Vector3f Vector3f::crossProduct( Vector3f rhs ) const {
	const float a1 = getX();
	const float a2 = getY();
	const float a3 = getZ();
	const float b1 = rhs.getX();
	const float b2 = rhs.getY();
	const float b3 = rhs.getZ();
	const float i = a2*b3 - a3*b2;
	const float j = a3*b1 - a1*b3;
	const float k = a1*b2 - a2*b1;
	return Vector3f( i, j, k );
}

Vector3f Vector3f::abs() const {
	const float x = (getX() >= 0) ? getX() : -getX();
	const float y = (getY() >= 0) ? getY() : -getY();
	const float z = (getZ() >= 0) ? getZ() : -getZ();
	return Vector3f( x, y, z );
}

float Vector3f::distance( Vector3f rhs ) const {
	const float diffX = getX() - rhs.getX();
	const float diffY = getY() - rhs.getY();
	const float diffZ = getZ() - rhs.getZ();
	return sqrt( diffX * diffX + diffY * diffY + diffZ * diffZ );
}


}
